#include <iostream>
#include <math.h>

#include "gl.h"
#include "sdl.h"
#include "sdl_image.h"

#include "GameState.h"
#include "GameMap.h"
#include "SDLCanvas.h"
#include "GLContext.h"
#include "GLTextures.h"
#include "GLScene.h"
#include "GLPickNumberOfPlayersScene.h"
#include "GLRegisterUsersScene.h"
#include "Controller.h"


#include<iostream>
#include<string>
#include<cstdlib>			//used for random
#include <ctime>			//used for time from random
#include <vector>
#include"ResourceSet.h"
#include"Player.h"
#include"Game.h"
#include"ConstantType.h"

using namespace catan;

int main(int argc, char **argv)
{
	GameState gameState;
	GameMap gameMap;
	SDLCanvas sdlCanvas(gameState);
	GLTextures glTextures(gameState);
	GLContext glContext(gameState);
	GLScene glScene(gameState, sdlCanvas, glTextures, gameMap);
	GLPickNumberOfPlayersScene glPickNumberOfPlayersScene(gameState, sdlCanvas, glTextures, gameMap);
	GLRegisterUsersScene glRegisterUsersScene(gameState, sdlCanvas, glTextures, gameMap);

	sdlCanvas.updateViewport();


#ifdef Claudiu
/*	comment the following for live demo
	gameState.setScreenNumber(4);					//hard coded temporary for 3 players
	gameState.setNumberOfPlayers(4);
	gameState.addPlayer(Player("Claudiu",1.0f, 0.0f, 0.0f));
	gameState.addPlayer(Player("Michel",0.0f, 0.0f, 1.0f));
	gameState.addPlayer(Player("Dan",0.0f, 1.0f, 0.0f));
	gameState.addPlayer(Player("Joey",1.0f, 1.0f, 0.0f));
	gameState.setIsPreStartGame(false);
*/
#endif

	bool isActive = true;
	while (gameState.isRunning())
	{
		SDL_Event event;
		while(SDL_PollEvent(&event))
		{
		    switch(event.type)
			{
			case SDL_ACTIVEEVENT:
				Controller::handleActiveEvent(event, isActive);
				break;
			case SDL_VIDEORESIZE:
				Controller::handleVideoResize(event, sdlCanvas, glTextures, glContext);
				break;
			case SDL_KEYDOWN:
				Controller::handleKeyDown(event, gameState);
				break;
			case SDL_MOUSEBUTTONDOWN:
				Controller::handleMouseButtonDown(event, gameState);
				break;
			case SDL_MOUSEMOTION:
				Controller::handleMouseMotion(event, gameState);
				break;
			case SDL_QUIT:
				Controller::handleQuit(gameState);
				break;
			}
		}
	    if (isActive && gameState.isRunning())
	    {
	    	switch (gameState.getScreenNumber())
	    	{
	    	case 0:
		    	glPickNumberOfPlayersScene.poll();
		    	glPickNumberOfPlayersScene.draw();
		    	break;
	    	case 1:
	    		glRegisterUsersScene.poll();
	    		glRegisterUsersScene.draw();
	    		break;
	    	default:
	    		glScene.poll();
	    		glScene.draw();
	    	}
	    }
	}
	std::cout << "Exited naturally." << std::endl;
	return 0;
}
